package xx.iverson.school_manage.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xx.iverson.school_manage.entity.User;
import xx.iverson.school_manage.send.SendSms;
import xx.iverson.school_manage.service.PointsService;
import xx.iverson.school_manage.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author wei77
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private PointsService pointsService;


    // 登录
    @RequestMapping("login")
    public String login(HttpServletRequest request, HttpSession session, Model model) throws JsonProcessingException {
        boolean num = userService.login(request.getParameter("username"), request.getParameter("password"), request.getParameter("role"));
        if (num) {
            User user = userService.getUser(request.getParameter("username"));
            session.setAttribute("user", userService.getUser(request.getParameter("username")));
            session.setAttribute("point", pointsService.getPoints(user.getUserId()));
            // 判断是否为管理员
            if ("1".equals(user.getRole())) {
                return "redirect:toIndex";
            }
            if ("2".equals(user.getRole())) {
                return "redirect:toHeadIndex";
            } else {
                return "redirect:toAdminIndex";
            }
        } else {
            // 设置为-1表示登录失败
            model.addAttribute("flag", -1);
            return "login";
        }
    }

    // 验证码登录
    @RequestMapping("toCodeLogin")
    public String toCodeLogin() throws Exception {
        return "code login";
    }

    // 发送验证码
    @RequestMapping("sendCode")
    @ResponseBody
    public String sendCode(@RequestParam("username")String phone, HttpSession session) throws Exception {
        int userInt = userService.checkUserName(phone);
        if (userInt > 0) {
            String code = SendSms.generateCaptcha(6);
            String result = SendSms.send(phone, "6015", code);
            if (result.equals("发送成功")) {
                session.setAttribute(phone, code);
                return  "200";
            } else {
                return  "-3";
            }
        } else {
            return  "-2";
        }
    }

    // 验证码登录
    @RequestMapping("codeLogin")
    public String codeLogin(HttpServletRequest request, HttpSession session, Model model) throws Exception {
        String phone = request.getParameter("username");
        String code = request.getParameter("code");
        String codeSystem = (String) session.getAttribute(phone);
        if (code.equals(codeSystem)) {
            User user = userService.getUser(request.getParameter("username"));
            session.setAttribute("user", userService.getUser(request.getParameter("username")));
            session.setAttribute("point", pointsService.getPoints(user.getUserId()));
            return "redirect:toIndex";
        } else {
            // 设置为-1表示登录失败
            model.addAttribute("flag", -1);
            return "code login";
        }
    }

    // 添加用户
    @RequestMapping("addUser")
    @ResponseBody
    public Boolean addUser(@RequestParam("username") String username,
                           @RequestParam("role") String role,
                           @RequestParam("realName") String realName,
                           @RequestParam("letter") String letter,
                           @RequestParam("classId") Integer classId) {
        return userService.addUser(username, role, realName, letter, classId);
    }

    //查询用户
    @RequestMapping("selectUserByClass")
    @ResponseBody
    public List<Integer> selectUserByClass(String grade) {
        return userService.getUserByClass(grade);
    }

    //添加用户验证
    @RequestMapping("checkUserName")
    @ResponseBody
    public int checkUserName(@RequestParam("username") String username) {
        if (userService.checkUserName(username) > 0) {
            return 1;
        }
        return 0;
    }

    // 删除用户
    @RequestMapping("deleteUser")
    @ResponseBody
    public Boolean deleteUser(Integer id) {
        return userService.removeUser(id);
    }

    // 重置密码
    @RequestMapping("updateUserPassword")
    @ResponseBody
    public Boolean updateUserPassword(Integer id) {
        return userService.modifyUserPassword(id);
    }

    // 重置密码
    @RequestMapping("updateUserClass")
    @ResponseBody
    public Boolean updateUserClass(Integer id, Integer grade) {
        return userService.modifyUserClass(id, grade);
    }

    // 查询科室
    @RequestMapping("getDepartment")
    @ResponseBody
    public List<User> getDepartments() {
        return userService.getDepartments();
    }

    // 修改密码
    @RequestMapping("updatePassword")
    @ResponseBody
    public Boolean updatePassword(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (Objects.equals(user.getPassword(), request.getParameter("oldPassword"))) {
            if (userService.updatePassword(user.getUserId(), request.getParameter("newPassword"))) {
                session.removeAttribute("user");
                return true;
            }
        }
        return false;
    }

    @RequestMapping("logout")
    @ResponseBody
    public Boolean logout(HttpSession session) {
        session.removeAttribute("user");
        return true;
    }
}
